
package eu.itcsolutions.android.tutorial;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;



import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
 import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class AdaugaAgent extends Activity {
	
	// Data dm;
	// List<String[]> ordersToSend =null ;
	 //String[] StringOfOrders;

       // private final static String SERVICE_URI = "http://192.168.61.3/SalesService/SalesService.svc";
	private final static String SERVICE_URI = "http://sfa.pangram.ro:8090/PostgresWebService/rest";
        @SuppressWarnings("null")
		@Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);
               
                String isNot =new String("");
               
                Data dm = null;
                HttpGet request = new HttpGet(SERVICE_URI + "/sales/search/1");
                request.setHeader("Accept", "application/json");
                request.setHeader("Content-type", "application/json"); 
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String theString = new String("");
 
           
            
            try {
                HttpResponse response = httpClient.execute(request);
                HttpEntity responseEntity = response.getEntity();
                InputStream stream = responseEntity.getContent();
                BufferedReader reader = new BufferedReader(
                                        new InputStreamReader(stream));
              

                Vector<String> vectorOfStrings = new Vector<String>();
                String tempString = new String();
                String tempStringID     = new String();
                String tempStringuserName   = new String();
                String tempStringpassword  = new String();
                String tempStringzone = new String();
                
                
                StringBuilder builder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                                builder.append(line);
                        }
                stream.close();
                theString = builder.toString();
             
                JSONObject json=new JSONObject(theString);
                Log.i("userpasscheck","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
                Log.i("userPass","<UtilizatorID"+">"      +json.getString("id")    +"</UtilizatorID"    +">\n");
                Log.i("userPass","<Parola"+">"            +json.getString("password")          +"</Parola"          +">\n");
                
                dm = new Data (getApplicationContext());
               
                
                //JSONArray nameArray=json.getJSONArray("userPass");
                
                //for(int i=0;i<nameArray.length();i++)
                	
                //{
                	
                        //Log.i("userPass","<UtilizatorID"+i+">"      +nameArray.getJSONObject(i).getString("id")    +"</UtilizatorID"    +i+">\n");
                        //Log.i("userPass","<Parola"+i+">"            +nameArray.getJSONObject(i).getString("password")          +"</Parola"          +i+">\n");
                        
                        tempStringID     = json.getString("id");
                        tempStringuserName   = json.getString("password");
                        tempStringpassword  = json.getString("userName");
                        tempStringzone = json.getString("zone");

                 //       dm.insertIntoSetup(tempStringID,tempStringuserName,tempStringpassword,tempStringzone);
                        
                //}
               
            } catch (Exception e) {
                        e.printStackTrace();
                        Logger lgr = Logger.getLogger(AdaugaAgent.class.getName());
                        lgr.log(Level.SEVERE, e.getMessage(), e);
            }        
        }
}
                